/**
 * Created by wxf on 2020/9/11.
 */
$(function () {
    // 1.获取参数
    var queryWidgets = decodeURIComponent(getQueryString("queryWidgets"));
    //开始月份、结束月份、项目名称
    var arr = JSON.parse(queryWidgets);
    // var arr = [{"id":2939,"value":"2020-08"},{"id":2940,"value":"2020-09"},{"id":2926,"value":""}];
    var beginMonth="",endMonth="",xmmc="";
    console.log(arr);
    if(arr){
        for(var i=0;i<arr.length;i++){
            var id = arr[i].id,value = arr[i].value;
            switch(id){
                case 2939:
                    beginMonth=value;
                    break;
                case 2940:
                    endMonth=value;
                    break;
                case 2926:
                    xmmc=value;
            }
        }
    }
    var selectedPjData=[];
    //项目列表
    layui.use('table', function(){
        var $ = layui.$;
        var table = layui.table;
        var tableRender = table.render({
            elem: '#table'
            ,height:300
            ,even: true //开启隔行背景
            ,url:$ctx+'/pjProjectInfo/queryOpsByMonthAndName'
            ,where: {beginMonth:beginMonth, endMonth: endMonth,pjName:xmmc}
            ,cols: [[
                {field:'id', title: 'ID', hide: true}
                ,{field:'procode', title: '项目编码',width:150}
                ,{field:'name', title: '项目名称',width:150,sort:true}
                ,{field:'prjtype', title: '项目类型',width:90}
                ,{field:'xmjd', title: '项目阶段',width:90, hide: true} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                ,{field:'khmc', title: '客户名称',width:130}
                ,{field:'cpmc', title: '产品名称',width:150}
                ,{field:'htje', title: '合同总额',width:90}
                ,{field:'passnoworktime', title: '施工期限',width:90}
                ,{field:'xsjl', title: '销售经理',width:90}
                ,{field:'xmjl', title: '项目经理',width:90}
                ,{field:'ywjl', title: '运维经理',width:90}
                ,{field:'members', title: '项目成员',width:120}
                ,{field:'companyName', title: '所属公司',width:90}
                ,{field:'ssbm', title: '所属部门',width:90}
                ,{field:'ssjyt', title: '所属经营体',width:100}
                ,{field:'status', title: '项目状态',width:90, hide: true}
            ]]
        });
        //重载
        var active = {
            reload: function(){
                //执行重载
                table.reload('table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        beginMonth:beginMonth,
                        endMonth: endMonth,
                        pjName:xmmc
                    }
                }, 'data');
            }
        };
        //监听行单击事件
        table.on('row(layui-table)', function(obj){
            selectedPjData=obj.data;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            $("#xmxq").html(obj.data.name+"-项目详情");
            //查询项目KPI指标值
            getKpiSpanData(selectedPjData);
        });
    });

});

//查询项目KPI指标值
function getKpiSpanData(selectedPjData) {
    var xmbm=selectedPjData.procode;
    $.ajax({
        url: $ctx+'/pjProjectOps/getKpiByXmbm',
        // dataType: 'json',
        data:{
            "xmbm":xmbm
        },
        type: 'get',
        success: function (data) {
            var res=data.data;
            if(res){
                var profit=res["profit"];
                if(profit){
                    $("#jlr").html((profit.shared_profit?(profit.shared_profit/10000).toFixed(2):0)+"万");
                    $("#xssr").html((profit.sales?(profit.sales/10000).toFixed(2):0)+"万");
                    $("#zycb").html((profit.main_cost?(profit.main_cost/10000).toFixed(2):0)+"万");
                    $("#zjfy").html((profit.direct_expense?(profit.direct_expense/10000).toFixed(2):0)+"万");
                    $("#xjlje").html((profit.cash_flow?(profit.cash_flow/10000).toFixed(2):0)+"万");
                }
                var opt=res["opt"];
                if(opt){
                    $("#cbz").html(opt.dqxmsy?(opt.dqxmsy).toFixed(4):0);
                    $("#dqyxtt").html(opt.dqsbzts?opt.dqsbzts:0);
                    $("#jdl").html(opt.pjjdl?((opt.pjjdl).toFixed(2)+"%"):"0%");
                    $("#sbyzl").html(opt.sbyzl?((opt.sbyzl).toFixed(2)+"%"):"0%");
                }
                var problem=res["problem"];
                for(var i=0;i<problem.length;i++){
                    switch (problem[i].status_type){
                        case "质量事故":
                            $("#zlsg").html(problem[i].qty);
                            break;
                        case "客户投诉":
                            $("#khts").html(problem[i].qty);
                            break;
                    }
                }
            }
        }
    });
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURIComponent(decodeURIComponent(window.location.search)).substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

